﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using ImageAppViewModels;
using WpfImageDuplicates;

namespace CompareMultiple.Plugin
{
    public class SimilarityGroupHandler
    {
        public List<SimilarityGrouping> _groups;
        public int _currentGroup;

        public SimilarityGroupHandler()
        {
        }

        public void ClearGroups()
        {
            if (_groups != null)
            {
                for (int n = 0; n < _groups.Count; n++)
                {
                    var group = _groups[n];
                    group.Similar.Clear();                    
                }
            }
        }

        public void RegisterGroups(List<SimilarityGrouping> groups)
        {
            _currentGroup = 0;
            _groups = new List<SimilarityGrouping>(groups);
            _groups.Sort(new Comparison<SimilarityGrouping>((g1, g2) => g2.Similar.Count.CompareTo(g1.Similar.Count)));
        }

        public SimilarityGrouping CurrentGroup
        {
            get
            {
                if (_groups == null || _currentGroup >= _groups.Count)
                    return null;

                return _groups[_currentGroup];
            }
        }

        public bool OpenNext()
        {
        NextTry:
            var group = CurrentGroup;

            if (group == null)
                return false;

            group.Similar.Sort(new Comparison<FileImageViewModel>((vm1, vm2) => vm2.Blur.CompareTo(vm1.Blur)));

            bool onlyOneImage = Commands.WantCompareMultipleInNewWindow.Execute(group);

            _currentGroup++;

            if (onlyOneImage)
            {
                goto NextTry;
            }

            return true;
        }
    }
}
